﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Params;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class ParamsController : ControllerBase
    {
        #region Index
        //
        // GET: /Params/

        public ActionResult Index(ParamType? type)
        {
            IEnumerable<Param> parameters = null;
            try
            {
                ParamService.ParamServiceClient paramService = new ParamService.ParamServiceClient();
                paramService.AddValidationToken(this.Token);
                parameters = type.HasValue ? paramService.GetParamListByType(type.Value) : paramService.GetParamList();
                paramService.Close();
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorParamList, MessageType.Error);
            }
            return View(parameters);
        } 
        #endregion

        #region Details
        //
        // GET: /Params/Details/5

        public ActionResult Details(int id)
        {
            Param parameter = null;
            try
            {
                ParamService.ParamServiceClient paramService = new ParamService.ParamServiceClient();
                paramService.AddValidationToken(this.Token);
                parameter = paramService.GetParam(id);
                paramService.Close();
                return View(parameter);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.ParamNotFound, MessageType.Error);
                return RedirectToAction("Index");
            }
        } 
        #endregion

        #region Create
        //
        // GET: /Params/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Params/Create

        [HttpPost]
        public ActionResult Create(Param param)
        {
            try
            {
                ParamService.ParamServiceClient paramService = new ParamService.ParamServiceClient();
                paramService.AddValidationToken(this.Token);
                paramService.StoreParam(param);
                paramService.Close();
                this.SetMessage(InfoStrings.ParamCreated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorParamCreating, MessageType.Error);
            }
            return View(param);
        } 
        #endregion

        #region Edit
        //
        // GET: /Params/Edit/5

        public ActionResult Edit(int id)
        {
            return Details(id);
        }

        //
        // POST: /Params/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Param param)
        {
            try
            {
                param.Id = id;
                ParamService.ParamServiceClient paramService = new ParamService.ParamServiceClient();
                paramService.AddValidationToken(this.Token);
                paramService.StoreParam(param);
                paramService.Close();
                this.SetMessage(InfoStrings.ParamUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorParamUpdating, MessageType.Error);
                
            }
            return View(param);
        } 
        #endregion

        #region Delete
        //
        // GET: /Params/Delete/5

        public ActionResult Delete(int id)
        {
            return Details(id);
        }

        //
        // POST: /Params/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                ParamService.ParamServiceClient paramService = new ParamService.ParamServiceClient();
                paramService.AddValidationToken(this.Token);
                paramService.RemoveParam(id);
                paramService.Close();
                this.SetMessage(InfoStrings.ParamDeleted, MessageType.Info);
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorParamDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
